#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
极地海图 - 主程序

加载UI文件并显示主窗口
"""

import sys
import os
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QSplashScreen
from PyQt5.QtCore import Qt, QTimer
from PyQt5.uic import loadUi

# 加载配置和组件系统
from config import config
from menu import setup_menus
from context import context
from uitls.utils import get_ui, load_stylesheet

class MapApplication(QMainWindow):
    """极地海图主应用程序类"""
    
    def __init__(self):
        super(MapApplication, self).__init__()
        # 加载UI文件
        loadUi(get_ui("main.ui"), self)
        
        # 设置窗口大小
        self.resize(2000, 1200)

        # 设置窗口标题
        self.setWindowTitle(config['application.name'] + " - " + config['application.version'])
        
        # 加载样式
        load_stylesheet(self)

        self.init_ui()

    def init_ui(self):
        """初始化UI组件"""
        # 设置菜单
        setup_menus(self)

        # 获取并初始化地图组件
        map = context.get("map")
        map.init_ui(self)
        self.setCentralWidget(map)

        cluster = context.get("cluster")
        cluster.init_ui(self.clusterDock)

    def toggle_fullscreen(self):
        """切换全屏模式"""
        if self.isFullScreen():
            self.showNormal()
        else:
            self.showFullScreen()

def show_splash_screen():
    """显示启动画面"""
    splash_pix = QtGui.QPixmap(os.path.join(os.path.dirname(__file__), "resources", "splash.png"))
    splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
    splash.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
    splash.show()
    QApplication.processEvents()
    return splash


def main():
    """程序入口点"""
    # 创建应用
    app = QApplication(sys.argv)
    splash = show_splash_screen()

    from map import Map
    from cluster import Cluster
    from sensor import Sensor
    context.initialize()
    window = MapApplication()

    QTimer.singleShot(1000, lambda: {splash.close(), window.show()}) 

    # 进入应用主循环
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
